import React from 'react';
import {Spin, Card, Button, Table} from 'antd';
import PropTypes from 'prop-types';
import {getDetail} from './api';
import {injectIntl} from './intl';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {baseDataStatus} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {TABLE, DATETIME_FORMAT} from '../constants';
const {Description} = DescriptionList;

class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
        data: {},
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    render() {
        const {formatMessage} = this.props.intl;
        const data = this.state.data || {};
        const colums = [{
            title: formatMessage({
                id: 'newPanel.column.code',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'code',
        }, {
            title: formatMessage({
                id: 'newPanel.column.name',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'name',
        }];
        return (
            <div>
                <Spin spinning={this.state.loading}>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'detail.operation.detail',
                            defaultMessage: '基础信息'
                        })}>
                            <Description term={formatMessage({
                                id: 'detail.operation.code',
                                defaultMessage: '编号'
                            })}>{data.code}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.brandId',
                                defaultMessage: '品牌'
                            })}>{`(${data.brandCode})${data.brandName}`}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.dutyUnitCode',
                                defaultMessage: '原供应商编号'
                            })}>{data.dutyUnitCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.dutyUnitName',
                                defaultMessage: '原供应商名称'
                            })}>{data.dutyUnitName}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.replacedDutyUnitCode',
                                defaultMessage: '替换供应商编号'
                            })}>{data.replacedDutyUnitCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.replacedDutyUnitName',
                                defaultMessage: '替换供应商名称'
                            })}>{data.replacedDutyUnitName}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.startTime',
                                defaultMessage: '开始时间'
                            })}>{formatDateTime(data.startTime, DATETIME_FORMAT)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.endTime',
                                defaultMessage: '结束时间'
                            })}>{formatDateTime(data.endTime, DATETIME_FORMAT)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.remark',
                                defaultMessage: '备注'
                            })}>{data.remark}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.status',
                                defaultMessage: '状态'
                            })}>{conventEnumValueToString(baseDataStatus, data.status)}
                            </Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'detail.operation.details',
                            defaultMessage: '备件清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                rowKey="code"
                                columns={colums}
                                pagination={false}
                                dataSource={data.parts}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{formatMessage({
                        id: 'detail.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div >
        );
    }
}

DetailPanel.propTypes = {
    permissions: PropTypes.array.isRequired,
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
};


import {connect} from 'react-redux';
import {
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
